package com.zjc.yohoshop.mvp.view.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerClickListener;
import com.zjc.lib_core.base.BaseFragment;
import com.zjc.yohoshop.R;
import com.zjc.yohoshop.bean.SeeListEntity;
import com.zjc.yohoshop.di.compont.DaggerSeeListFragmentCompont;
import com.zjc.yohoshop.di.modules.SeeListFragmentModules;
import com.zjc.yohoshop.listener.HomeBannerImageLoader;
import com.zjc.yohoshop.mvp.contract.SeeListFragmentContract;
import com.zjc.yohoshop.mvp.persenter.SeeListFragmentPresenter;
import com.zjc.yohoshop.mvp.view.adapter.SeeListAdapter;
import com.zjc.yohoshop.network.API;

import java.util.ArrayList;
import java.util.List;

/**
 * 逛列表最新
 */
public class SeeListFragment extends BaseFragment<SeeListFragmentPresenter> implements SeeListFragmentContract.SeeListFragmentIView, BaseRefreshListener {

    private boolean brand = true;
    private RecyclerView rv_see_list;
    private PullToRefreshLayout ptl_see_list;
    private SeeListAdapter adapter;
    private Banner banner;
    private List<SeeListEntity.BannerBean> bannerBeans;

    @Override
    public View createView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_see_list, null);
        return view;
    }

    @Override
    public void init(View view) {
        ptl_see_list = view.findViewById(R.id.ptl_see_list);
        //最新头布局控件初始化
        View view_banner = View.inflate(getActivity(), R.layout.item_see_list_banner, null);
        banner = view_banner.findViewById(R.id.ban_item_see_list);
        //列表控件初始化
        rv_see_list = view.findViewById(R.id.rv_see_list);
        rv_see_list.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new SeeListAdapter(R.layout.item_see_list);

        adapter.addHeaderView(view_banner, 0);
        rv_see_list.setAdapter(adapter);

    }

    @Override
    public void initData() {
        p.request();
    }

    @Override
    public void setupDagger() {
        DaggerSeeListFragmentCompont.builder().seeListFragmentModules(new SeeListFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {

        ptl_see_list.setRefreshListener(this);
        //数据点击事件
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(getActivity(), ((SeeListAdapter)adapter).getData().get(position).see_news_title, Toast.LENGTH_SHORT).show();
            }
        });
        //banner点击事件
        banner.setOnBannerClickListener(new OnBannerClickListener() {
            @Override
            public void OnBannerClick(int position) {
                Toast.makeText(getActivity(), bannerBeans.get(position - 1).recommend_name, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void success(SeeListEntity entity) {
        if (entity.values == null || entity.values.size() == 0){
            Toast.makeText(getActivity(), "没数据了", Toast.LENGTH_SHORT).show();
            return;
        }
        Log.e("ZJC", "SeeListFragment -> success: -> " + entity.values.size());

        for (int i = 0; i < entity.values.size(); i++){
            entity.values.get(i).banner = entity.banner;
        }
        adapter.setNewData(entity.values);

        if (brand) {
            bannerBeans = entity.banner;
            List<String> strs = new ArrayList<>();
            for (SeeListEntity.BannerBean bannerBean : entity.banner) {
                strs.add(API.BaseUrl + bannerBean.recommend_url);
            }
            banner.setImages(strs);
            banner.setImageLoader(new HomeBannerImageLoader());
            banner.isAutoPlay(true);
            banner.setIndicatorGravity(BannerConfig.CENTER);
            banner.setDelayTime(1 * 1500);
            banner.start();
            brand = !brand;
        }
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void refresh() {
        adapter.getData().clear();
        p.refresh();
        ptl_see_list.finishRefresh();
    }

    @Override
    public void loadMore() {
        p.loadMore();
        ptl_see_list.finishLoadMore();
    }
}
